<?php
class isp_customer_kontrak_model extends MY_Model {
	var $model = 'isp_customer_kontrak';
	
	function GetJSON()
	{
		$ls_sql	= "	SELECT
					a.id_customer,
					b.no_id,
					b.kode_kontrak,
					a.kode_customer,
					a.status,
					a.nama as nama_customer,
					b.marketing_fee,
					b.harga,
					b.keterangan,
					TO_CHAR(b.tgl_awal_kontrak,'DD/MM/YYYY') tgl_awal_kontrak,
					TO_CHAR(b.tgl_akhir_kontrak,'DD/MM/YYYY') tgl_akhir_kontrak,
					TO_CHAR(a.tgl_aktifasi,'DD/MM/YYYY') tgl_aktifasi,
					b.kode_jenis_barang,
					c.nama_jenis_barang,
					c.bandwidth
					FROM	ISP_CUSTOMER a					
					LEFT JOIN ISP_CLIENT_KONTRAK b
					ON a.kode_customer = b.kode_customer
					LEFT JOIN ISP_PRODUK c
					ON b.kode_jenis_barang = c.id_produk
					WHERE lower(a.status) = lower('Berlangganan')
							and (UPPER(a.kode_customer) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.NAMA) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(b.KODE_KONTRAK) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(c.bandwidth) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(b.kode_jenis_barang) LIKE '%".strtoupper(post('keyword'))."%')
					ORDER BY ".(post('sort')=='' ? 'b.kode_kontrak' : post('sort'))." ".(post('order')=='' ? 'desc' : post('order'));
					
		return json_encode( $this->paging($ls_sql) );
	}
		
	function GetJSON2()
	{
		$ls_sql	= "	SELECT
					b.no_id,
					a.id_customer,
					b.kode_kontrak,
					a.kode_customer,
					a.nama as nama_customer,
					b.harga,
					c.nama_jenis_barang,
					c.bandwidth
					FROM	ISP_CUSTOMER a					
					LEFT JOIN ISP_CLIENT_KONTRAK b
					on a.kode_customer = b.kode_customer
					LEFT JOIN ISP_PRODUK c
					on b.kode_jenis_barang = c.id_produk
					WHERE lower(a.status) = lower('Berlangganan')
							and b.kode_kontrak is not null
							and (UPPER(a.kode_customer) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.nama) LIKE '%".strtoupper(post('keyword'))."%')
					ORDER BY ".(post('sort')=='' ? 'b.no_id' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));		
		return json_encode( $this->paging($ls_sql) );
	}

	function GetJSON4()
	{
		$ls_sql	= "	SELECT *
					FROM ISP_PRODUK
					WHERE   (UPPER(kode_jenis_barang) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(nama_jenis_barang) LIKE '%".strtoupper(post('keyword'))."%')
					ORDER BY ".(post('sort')=='' ? 'kode_jenis_barang' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));		
		return json_encode( $this->paging($ls_sql) );
	}

	function GetJSON3()
	{
		$ls_sql	=  "SELECT	*
					FROM	ISP_CUSTOMER
					WHERE	(UPPER(kode_customer) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(nama_customer) LIKE '%".strtoupper(post('keyword'))."%')
					ORDER BY ".(post('sort')=='' ? 'kode_customer' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
		return json_encode( $this->paging($ls_sql) );
	}

	function Add($arr_data){
		$this->db->insert('isp_client_kontrak', $arr_data);
	}
	
	function Update($arr_data, $arr_rule){
		$this->db->update('isp_client_kontrak', $arr_data, $arr_rule);
	}
	
	function GetSingle($as_no_id){
		$result = $this->db->get_where('isp_client_kontrak', array('no_id' => $as_no_id));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function GetSingleView($as_no_id)
	{
		$result = $this->db->get_where('isp_client_kontrak', array('no_id' => $as_no_id));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}

	function DeleteSingle($arr_rule)
	{
		try {
			if(	intval($this->get_data("SELECT COUNT(1) FROM isp_client_kontrak WHERE no_id = '".$arr_rule['no_id']."'"))<=0)
			{
				return $this->error_delete.' [id: '.$arr_rule['no_id']."]<br />";
			}else{
				$this->db->delete('isp_client_kontrak', array('no_id' => $arr_rule['no_id']));
				return '';
			}
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}
}
?>